package cn.hchaojie.blog.web.demo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.hchaojie.blog.domain.AjaxResponse;
import cn.hchaojie.blog.domain.Category;

/**
 * servlet2.5, 3.0
 * 从3.0开始，支持注解方式配置servlet映射，不需要通过web.xml
 * 
 * @author hchaojie
 *
 */
@WebServlet(urlPatterns = "/table_data")
public class TableServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("table servlet...");
		
		List<Category> list = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			Category c = new Category();
			c.setId(i);
			c.setName("分类" + i);
			c.setPriority(i);
			
			list.add(c);
		}
		AjaxResponse reponse = new AjaxResponse();
		reponse.setCode(0);
		reponse.setCount(100);	// 数据库一共多少条记录，（分页用）
		reponse.setMsg("");
		reponse.setData(list);
		
		String string = JSON.toJSONString(reponse);
		
		resp.setContentType("application/json;charset=utf-8");
		resp.getWriter().write(string);
	}
}
